import * as THREE from "three";
import * as CANNON from 'cannon-es';

console.log("----", new CANNON.Vec3())

export function createGeometry(obj: any) {
    switch (obj.name) {
        case "BoxGeometry":
            return getBoxGeometry(obj.param);
        case "SphereGeometry":
            return getSphereGeometry(obj.param);
        case "CylinderGeometry":
            return getCylinderGeometry(obj.param);

    }
    return null;
}

export function createBody(obj: any) {
    switch (obj.name) {
        case "Plane":
            return getPlaneBody();
        case "Box":
            return getBoxBody(obj.param);
        case "Cylinder":
            return getCylinderBody(obj.param);
        case "Sphere":
            return getSphereBody(obj.param);

    }
    return null;
}

function getPlaneBody() {
    const shape = new CANNON.Plane();
    const chassisBody = new CANNON.Body({ mass: 0 })
    chassisBody.addShape(shape);
    return chassisBody;
}

function getCylinderBody(param: any) {
    const {
        radiusTop,
        radiusBottom,
        height,
        numSegments,
        mass
    } = param;
    const shape = new CANNON.Cylinder(radiusTop, radiusBottom, height, numSegments);
    // const shape = new CANNON.Cylinder();
    const q = new CANNON.Quaternion();
    // q.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), Math.PI / 2);
    console.log(shape)
    const chassisBody = new CANNON.Body({ mass })
    chassisBody.addShape(shape, new CANNON.Vec3(0, 0, 0), q);

    // let box = new CANNON.Body({ mass: 2 });
    // box.addShape(new CANNON.Box(new CANNON.Vec3(1, 0.2, 0.5)));
    // let car = new CANNON.RaycastVehicle({
    //     chassisBody: box
    // })
    // car.addWheel({})
    // console.log(car)
    return chassisBody;
}

function getSphereBody(param: any) {
    const {
        radius,
        mass
    } = param;
    const shape = new CANNON.Cylinder(radius);
    const chassisBody = new CANNON.Body({ mass })
    chassisBody.addShape(shape);
    return chassisBody;
}

function getBoxBody(param: any) {
    const {
        x,
        y,
        z,
        mass
    } = param;
    const shape = new CANNON.Box(new CANNON.Vec3(x / 2, y / 2, z / 2));
    const chassisBody = new CANNON.Body({ mass })
    chassisBody.addShape(shape);
    return chassisBody;
}


function getBoxGeometry(param: any) {
    const {
        width,
        height,
        depth,
        widthSegments,
        heightSegments,
        depthSegments,
    } = param;
    return new THREE.BoxGeometry(width,
        height,
        depth,
        widthSegments,
        heightSegments,
        depthSegments)
}

function getSphereGeometry(param: any) {
    const {
        radius,
        widthSegments,
        heightSegments,
        phiStart,
        phiLength,
        thetaStart,
        thetaLength
    } = param;
    return new THREE.SphereGeometry(radius,
        widthSegments,
        heightSegments,
        phiStart,
        phiLength,
        thetaStart,
        thetaLength)
}

function getCylinderGeometry(param: any) {
    const {
        radiusTop,
        radiusBottom,
        height,
        radialSegments,
        heightSegments,
        openEnded,
        thetaStart,
        thetaLength,
    } = param;
    return new THREE.CylinderGeometry(radiusTop,
        radiusBottom,
        height,
        radialSegments,
        heightSegments,
        Boolean(openEnded),
        thetaStart,
        thetaLength,)
}